package cc.orange.mainView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import cc.orange.R;
import cc.orange.adapter.HomeAdapter;
import cc.orange.adapter.RankAdapter;
import cc.orange.base.BaseActivity;
import cc.orange.databinding.ActivityRankingBinding;
import cc.orange.databinding.ActivityRankingListBinding;
import cc.orange.entity.HightEntity;
import cc.orange.entity.MsgInfosVideoEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.StatusBarUtilView;
import cc.orange.utils.ZXToastUtil;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RankingActivity extends BaseActivity {
    private ActivityRankingBinding binding;
    private RankAdapter adapter;
    private ArrayList<MsgInfosVideoEntity.Data> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_ranking);
        initView();
        getSpeedVideo();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initView() {

        binding.rankingRecycler.setLayoutManager(new LinearLayoutManager(RankingActivity.this));
        adapter = new RankAdapter(list);
        binding.rankingRecycler.setAdapter(adapter);

        binding.msgaRel5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        StatusBarUtilView.transparencyBar(RankingActivity.this);
        StatusBarUtilView.setLightStatusBar(RankingActivity.this, true, true);

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) binding.msgaRel5.getLayoutParams();
        layoutParams.setMargins(30,getStatusBarHeight()+20,0,0);
        binding.msgaRel5.setLayoutParams(layoutParams);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getHight(HightEntity entity) {//改变布局
        if(entity.getHight() > 0){
            RelativeLayout.LayoutParams pp = new RelativeLayout.
                    LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    entity.getHight());
            binding.rankTitle.setLayoutParams(pp);
        }
    }

    private void getSpeedVideo() {
        ZXSharedPrefUtil ss = new ZXSharedPrefUtil();
        String userId = ss.getString("id_talk", "");
        RetrofitFactory.create(ApiService.class)
                .getSpeedVideo(getTokens(),"10", userId)
                .enqueue(new DefaultCallback<MsgInfosVideoEntity>() {
                    @Override
                    public void onStart(Call<MsgInfosVideoEntity> call) {
                        showLoading_base(RankingActivity.this);
                    }

                    @Override
                    public void onError(Call<MsgInfosVideoEntity> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<MsgInfosVideoEntity> call, MsgInfosVideoEntity msgInfosVideoEntity) {
                        cancelLoading();
                        if(msgInfosVideoEntity.getCode() == 0){

                            if(msgInfosVideoEntity.getData().size() > 3){
                                List<MsgInfosVideoEntity.Data> data = msgInfosVideoEntity.getData().subList(3, msgInfosVideoEntity.getData().size() - 1);
                                list.addAll(data);
                                initView3(msgInfosVideoEntity);
                            }else{
                                list.clear();
                            }
                            if(list.size() == 0){
                                ZXToastUtil.showToast("暂无数据");
                            }
                        }else{
                            ZXToastUtil.showToast(msgInfosVideoEntity.getMsg());
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    private void initView3(MsgInfosVideoEntity msgInfosVideoEntity) {
        if(msgInfosVideoEntity.getData().size() > 0){
            Glide.with(RankingActivity.this).load(msgInfosVideoEntity.getData().get(0).getPhoto())
                    .into(binding.rankingImg1);
            binding.rankingText1.setText(msgInfosVideoEntity.getData().get(0).getWomanName());
            int i1 = msgInfosVideoEntity.getData().size();
            double v = i1 * 1.67;
            binding.rankingText31.setText(""+new DecimalFormat("0.00").format(v)+"w");
        }

        if(msgInfosVideoEntity.getData().size() > 1){
            Glide.with(RankingActivity.this).load(msgInfosVideoEntity.getData().get(1).getPhoto())
                    .into(binding.rankingImg2);
            binding.rankingText2.setText(msgInfosVideoEntity.getData().get(1).getWomanName());
            int i1 = msgInfosVideoEntity.getData().size() - 1;
            double v = i1 * 1.67;
            binding.rankingText3.setText(""+new DecimalFormat("0.00").format(v)+"w");
        }

        if(msgInfosVideoEntity.getData().size() > 2){
            Glide.with(RankingActivity.this).load(msgInfosVideoEntity.getData().get(2).getPhoto())
                    .into(binding.rankingImg3);
            binding.rankingText4.setText(msgInfosVideoEntity.getData().get(2).getWomanName());
            int i1 = msgInfosVideoEntity.getData().size() - 2;
            double v = i1 * 1.67;
            binding.rankingText5.setText(""+new DecimalFormat("0.00").format(v)+"w");
        }
    }
}